package com.testmanager.testmananger.shuiyou.strCompare.service;

import com.testmanager.testmananger.foundation.dto.PageableDto;
import com.testmanager.testmananger.foundation.mapper.PageBaseMapper;
import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyDbManagerEntity;
import com.testmanager.testmananger.shuiyou.strCompare.dto.DbManagerSearchDto;
import com.testmanager.testmananger.shuiyou.strCompare.respository.SyDbManagerCustom;
import com.testmanager.testmananger.shuiyou.strCompare.respository.SyDbManagerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;
import java.sql.DriverManager;
import java.util.List;

@Service
@Slf4j
public class SyDbManagerService {

    @Autowired
    SyDbManagerCustom dbManagerCustom;
    @Autowired
    SyDbManagerRepository dbManagerRepository;
    @Autowired
    FlakeIdGenerator flakeIdGenerator;
    public ResponseEntity search(DbManagerSearchDto dbManagerSearchDto){
        log.info("SyDbManagerService search {}",dbManagerSearchDto);
        Page<SyDbManagerEntity> page = dbManagerCustom.search(dbManagerSearchDto);
        log.info("pageContent:{}",page);
        PageableDto pageableDto = PageBaseMapper.pageExchange(page, page.getContent());

        return  ResponseEntity.ok(pageableDto);

    }

    public ResponseEntity getDbsByProjectId(long projectId){
        List<SyDbManagerEntity> allByProjectId = dbManagerRepository.findAllByProjectId(projectId);
        return ResponseEntity.ok(allByProjectId);
    }


    public ResponseEntity saveOrUpdate(SyDbManagerEntity syDbManagerEntity){
       if ( !verify(syDbManagerEntity)){
           return ResponseEntity.status(501).body("数据库信息错误！");
       }

        if (null == syDbManagerEntity.getDbno()){
            syDbManagerEntity.setDbno("db"+ flakeIdGenerator.newId());
        }
        if (0 == syDbManagerEntity.getId()){
            syDbManagerEntity.setId(flakeIdGenerator.newId());
        }
       dbManagerRepository.save(syDbManagerEntity);
       return ResponseEntity.ok("success");

    }

    public ResponseEntity getAllDbs(){
        List<SyDbManagerEntity> all = dbManagerRepository.findAll();
        return ResponseEntity.ok(all);
    }


    public ResponseEntity verifycheck(SyDbManagerEntity dbManagerEntity){

        try { // 排除连接不上的错误
            Class.forName(dbManagerEntity.getDriverClass());
            DriverManager.getConnection(dbManagerEntity.getUrl(), dbManagerEntity.getUsername(), dbManagerEntity.getPassword());// 相当于连接数据库
            return ResponseEntity.ok("连接正常");
        } catch (Exception e) {
            //	throw new Exception(e);
            log.error("errer43:{}",e);
            return ResponseEntity.ok("不能正常连接");
        }


    }


    public boolean verify(SyDbManagerEntity dbManagerEntity){

        try { // 排除连接不上的错误
            Class.forName(dbManagerEntity.getDriverClass());
            DriverManager.getConnection(dbManagerEntity.getUrl(), dbManagerEntity.getUsername(), dbManagerEntity.getPassword());// 相当于连接数据库
            return true;
        } catch (Exception e) {
            //	throw new Exception(e);
            log.error("errer43:{}",e);
            return false;
        }


    }


}
